<?php
declare(strict_types=1);
namespace Service\ActivityService;
use Service\BaseService;
use App\Model\BbcModel\ActivityGoods;

/**
 * 活动商品表服务中心
 * @author chenshubo
 */
class ActivityGoodsService extends BaseService
{
    /**
     * 获取活动明细
     * @param array $actIds
     */
    public function getActDataList( array $actIds = [] )
    {
        if( empty($actIds) ) {
            return [];
        }
        return ActivityGoods::instance()->whereIn('activity_id',$actIds)->get()->toArray();
    }

    /**
     * 检查活动商品价格
     * @param int $cid
     * @param int $mn_sid
     * @param int $mid
     * @param array $param
     * @return mixed
     */
    public function checkGoodsPriceList(int $cid,int $mn_sid,int $mid,array $param,array $pushTemplateListMap)
    {

        $goods = $param['baseGoods'];
        $goods['brand_name'] = $param['brand_name'] ?? '';
        $baseSpecPriceList = $param['baseGoodsSpecPrice'];
        $batch = current($param['baseGoodsBatch']);
        $baseGoodsDistributorPrice = $param['baseGoodsDistributorPrice'];

        if( $goods['fictitious_sale'] > $goods['num_sale']){
            $goods['num_sale'] = $goods['fictitious_sale'];
        }
        unset($goods['fictitious_sale']);


        //处理规格问题
        $goods['spec_list'] = null;
        $baseSpec = 1;
        if(!empty($batch['spec_list']))
        {
            $spec_list  = @json_decode($batch['spec_list'],true);
            $baseSpec   = $spec_list[0]['spec_num']??1;
            $goods['price_market'] = $spec_list[0]['priceMarket'] ?? $goods['price_market'];
            foreach ($spec_list as $k=>$v ){
                if(!isset($v['specName'])){
                    $spec_list[$k]['specName']=$v['spec_num'].'件装';
                }
            }
            $goods['spec_list'] = array_column($spec_list,'spec_num','specName');
        }

        //处理基准价格问题
        if( !empty($baseSpecPriceList[$baseSpec]) ) {
            $spec_info = $baseSpecPriceList[$baseSpec];
            $goods['price_recommend']   = $spec_info['price_recommend'];
            $goods['price_store']       = $spec_info['price_store'];
        }


        $goods['activity'] = null;
        //是否使用推荐价格零售价
        if( $param['price_type'] == 1 )
        {
            $goods['price'] = $goods['price_recommend'];
        }
        else
        {
            //分销商等级价格设置
            if( $param['level'] > 0 )
            {
                $mn_user_level = $param['level'];
                if( isset($baseGoodsDistributorPrice[$mn_user_level]) && isset($baseGoodsDistributorPrice[$mn_user_level][$baseSpec]) ) {
                    $levelPrice = $baseGoodsDistributorPrice[$mn_user_level][$baseSpec];
                    $goods['price_store'] = $levelPrice['price'];
                }
            }
            $goods['price'] = $goods['price_store'];
        }

        //计算报税品不含价格有税率的情况
        if( $goods['is_bonded'] == 1 && $goods['tax_free'] == 0 ) {
            $goods['price']= bcadd($goods['price'],bcmul($goods['price'], $goods['tax_rate'],2),2);
        }

        if( $param['settingPrice'] == 0 ){
            $goods['price'] = upDecimal($goods['price'],1);
        }else{
            $goods['price'] = ceil($goods['price']);
        }

        /*处理微信推送模板*/
        if(count($pushTemplateListMap) > 0 && isset($pushTemplateListMap[$goods['activity_id']])){
            $goods['template_id'] = $pushTemplateListMap[$goods['activity_id']];
        }

        //类型：(1:整单促销，2商品促销 3-秒杀 4-拼团)
        if( $param['type'] == 4 )
        {
            $book           = current($param['channelBooking']);
            $number         = explode(',',$book['num']);
            $bookPriceStep  = [];
            $lowPrice=0;
            foreach ($number as $k => $v )
            {
                switch ($k){
                    case 0;
                    default:
                        $bookPriceStep[$v]=$param['price'];
                        if(($lowPrice>0 && $param['price']<$lowPrice) || $lowPrice==0){
                            $lowPrice=$param['price'];
                        }
                        break;
                    case 1:
                        $bookPriceStep[$v]=$param['price_second'];
                        if(($lowPrice>0 && $param['price_second']<$lowPrice) || $lowPrice==0){
                            $lowPrice=$param['price_second'];
                        }
                        break;
                    case 2:
                        $bookPriceStep[$v]=$param['price_third'];
                        if(($lowPrice>0 && $param['price_third']<$lowPrice) || $lowPrice==0){
                            $lowPrice=$param['price_third'];
                        }
                        break;
                    case 3:
                        $bookPriceStep[$v]=$param['price_forth'];
                        if(($lowPrice>0 && $param['price_forth']<$lowPrice) || $lowPrice==0){
                            $lowPrice=$param['price_forth'];
                        }
                        break;
                }
            }
            $goods['price_book']=$lowPrice;
            $goods['price_book_step']=$bookPriceStep;
        }
        /*秒杀活动*/
        if($param['type'] == 3){
            $goods['price_seckill'] = $param['price'];
        }
        return $goods;
    }
}